<template>
  <div style="background-color: bisque;width: calc(100% - 200px);height: calc(100VH - 100px);float: left">
    <h3 v-text="$route.query.id!=null?'编辑页面':'新增页面'"></h3>
    <span v-if="$route.query.id!=null">id:{{$route.query.id}}</span><br/>

    用户名:<input type="text" v-model="userName"><br/>
    密码:<input type="text" v-model="password" ><br/>
    <button @click="save">提交</button>
  </div>
</template>

<script>
export default {
  name: "add",
  data(){
    return{
      id:0,
      userName:'',
      password:''
    }
  },
  methods:{
    save(){
      var self = this
      this.$http({
        url: '/user/save',
        method: 'post',
        data: {
          id:self.id,
          userName:self.userName,
          password:self.password
        }
      })
          .then(function (res) {
            if(res.data){
              self.$router.push('/userlist')
            }
          })
    }
  },
  created() {
    this.id = this.$route.query.id
    var self = this
    if(this.id!=0){
      this.$http.get('/user/getById?id='+this.id).then(function(res){
        self.userName = res.data.userName
        self.password = res.data.password
      })
    }

  }
}
</script>

<style scoped>

</style>